using UnityEngine;
using UnityEngine.SceneManagement;

public class WizardActions : MonoBehaviour
{
	[Header("Audio Setup")]
	public AudioSource actionSource;
	public AudioClip jumpClip;
	public AudioClip deathClip;

	private bool isDead = false; // Prevents sound glitches after dying

	void Update()
	{
		// Only allow jumping if the wizard is still alive
		if (!isDead && Input.GetKeyDown(KeyCode.Space))
		{
			// PlayOneShot allows this to layer over the walking loop
			actionSource.PlayOneShot(jumpClip);
		}
	}

	// This fires when the Wizard's collider enters a Trigger collider
	private void OnTriggerEnter2D(Collider2D other)
	{
		// Check for the "Spikes" tag (Matches your Inspector exactly)
		if (!isDead && other.CompareTag("Spikes"))
		{
			Die();
		}
	}

	void Die()
	{
		isDead = true;

		// 1. Play the death sound immediately
		actionSource.PlayOneShot(deathClip);

		// 2. Disable physics so the wizard stops moving/falling
		Rigidbody2D rb = GetComponent<Rigidbody2D>();
		if (rb != null)
		{
			rb.simulated = false;
		}

		// 3. Wait 1.5 seconds so the "Huhh" sound finishes before reload
		Invoke("RestartLevel", 1.5f);
	}

	void RestartLevel()
	{
		// Reloads the scene you are currently in
		SceneManager.LoadScene(SceneManager.GetActiveScene().name);
	}
}